package hn.cch.sync_data.provider;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SyncDataProvider {


    public String selectData(String tableName, List<String> columnList, String whereSql, String orderSql) {
        String columnSql = String.join(", ", columnList);
        String sql = String.format("select %s from %s %s %s", columnSql, tableName, whereSql, orderSql);
        return sql;
    }

    public String deleteData(String tableName, String whereSql) {
        String sql = String.format("delete from %s %s ", tableName, whereSql);
        return sql;
    }

    public List<String> parseData(List<String> columnList, Map<String, Object> map) {
        List<String> data = new ArrayList<>();
        for (String column : columnList) {
            Object value = map.get(column);
            if (value == null) {
                data.add(String.format("%s", "null"));
            } else if (value instanceof String) {
                data.add(String.format("'%s'", value));
            } else if (value instanceof Number) {
                data.add(String.valueOf(value));
            } else if (value instanceof Timestamp) {
                Timestamp timestamp = (Timestamp) value;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sdf.format(timestamp);
                data.add(String.format("to_date('%s', 'yyyy-mm-dd hh24:mi:ss')", format));
            } else {
                data.add(String.valueOf(value));
            }
        }
        return data;
    }

    public String insertData(String tableName, List<String> columnList, List<Map<String, Object>> list, List<String> ignoreList) {
        // ignore字段
        columnList.removeAll(ignoreList);

        List<String> valueList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            List<String> data = parseData(columnList, map);
            String temp = String.join(", ", data);
            String columnSql = String.join(", ", columnList);
            String item = String.format(" into %s(%s) values (%s)", tableName, columnSql, temp);
            valueList.add(item);
        }
        String sql = String.format("insert all %s select 1 from dual", String.join("\n", valueList));
        return sql;
    }

    public String mergeData(String tableName, List<String> columnList, List<Map<String, Object>> list, List<String> uniqueList, List<String> ignoreList) {

        // ignore字段
        columnList.removeAll(ignoreList);
        List<String> valueList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            List<String> data = parseData(columnList, map);
            String temp = String.join(", ", data);
            String item = String.format(" select %s from dual ", temp);
            valueList.add(item);
        }
        // 组装sql
        String uniqueSql = uniqueList.stream().map(p -> String.format(" t0.%s = t1.%s ", p, p)).collect(Collectors.joining(" and "));

        // update字段
        List<String> updateList = new ArrayList<>(columnList);
        updateList.removeAll(uniqueList);

        String updateSql = String.format(" \nupdate set %s ",
                updateList.stream().map(p -> String.format(" t0.%s = t1.%s", p, p)).collect(Collectors.joining(", ")));

        String columnSql = String.join(", ", columnList);
        String insertSql = String.format(" \ninsert (%s) \nvalues (%s)", columnSql,
                columnList.stream().map(p -> String.format(" t1.%s ", p)).collect(Collectors.joining(", ")));


        String sql = String.format("merge into %s t0 using (\n%s\n) t1 on (%s) \nwhen matched then %s  \nwhen not matched then %s ",
                tableName, String.join(" union\n", valueList), uniqueSql, updateSql, insertSql);
        return sql;
    }


}
